import MainController from "./MainController";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class Barrier extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property(cc.Boolean)
    isAddBuffBall: boolean = false;

    // 剩余碰撞次数
    score: number = 1;

    // 对象
    main: MainController;

    onLoad() {
        // this.getRandomScore(5);
        // this.updateScore(this.score);
    }

    start() {
        if (this.label) {
            this.label.node.rotation = -this.node.rotation;
        }
    }

    // update (dt) {}

    /**
     * 更新碰撞次数
     */
    updateScore(score: number) {
        if(score == 0){return;}
        if (this.label) {
            this.score = score;
            this.label.string = score.toString();
        }
    }


    onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider): void {
        // cc.log('onBeginContact')
        if (this.isAddBuffBall) {
            this.main.addBall(this.node.position);
            this.main.removeBarrier(this);
        } else {
            this.updateScore(--this.score);
            this.main.addScore();
            if (this.score == 0) {
                this.main.removeBarrier(this);
            }
        }

    }
    onEndContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider): void {
        // cc.log('onEndContact')
    }

    onPreSolve(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider): void {
        // cc.log('onPreSolve')
    }
    onPostSolve(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider): void {
        // cc.log('onPostSolve')
    }

    // 随机分值
    getRandomScore(value: number) {
        if(value == 0){ value += 1}
        if (this.label) {
            this.score = value + Math.floor(Math.random() * 10);
            this.label.string = this.score.toString();
        }

    }

}
